﻿using UnityEngine;
using System.Collections;

public static class Vector2Extension {

    /// <summary>
    /// 控制最大长度
    /// </summary>
    /// <param name="v">Vector2</param>
    /// <param name="max">最大长度</param>
    public static void Truncate(this Vector2 v, float max)
    {
        if (v.sqrMagnitude > max * max)
        {
            v.Normalize();
            v *= max;
        }
    }

    /// <summary>
    /// 垂线 Perpendicular
    /// </summary>
    /// <param name="v">Vector2</param>
    /// <returns>垂线向量</returns>
    public static Vector2 Perp(this Vector2 v)
    {
        return new Vector2(-v.y, v.x);
    }

    /// <summary>
    /// 点乘
    /// </summary>
    /// <param name="v">Vector2</param>
    /// <param name="rh">点乘对象</param>
    /// <returns>点乘值</returns>
    public static float Dot(this Vector2 v, Vector2 rh)
    {
        return Vector2.Dot(v, rh);
    }
}
